<?php

$css = array('add_evaluacion.css');
$javascript = array('add_evaluacion.js');

flep_structure($structure, 'evaluacion');

if(isset($_POST['id'])){
    $structure['where'][0] = sprintf("`curso_id`='%s'", flep_decode($_POST['id']));
} else {
    header('Location: main.php?page=curso');
    exit();
}

function include_page($structure){
    $user = &$_SESSION['user'];
    if(isset($_SESSION['msg']) && $_SESSION['msg'] !== null){
        printf('<p class="msg">%s</p><br>', $_SESSION['msg']);
        $_SESSION['msg'] = null;
    }
    if(isset($_SESSION['error']) && $_SESSION['error'] !== null){
        printf('<p class="error">%s</p><br>', $_SESSION['error']);
        $_SESSION['error'] = null;
    }
    flep_structure($curso, 'curso');
    $curso['where'][0] = sprintf("`cursos`.`id`='%s'", flep_decode($_POST['id']));
    $curso['alias'][0] = '*';
    $curso['alias'][1] = '`docentes`.`nombre`AS docente';
    flep_search_all($curso);
    $found = flep_search_all($structure)
?><br>
    <table class="header2">
        <tbody>
            <tr><th colspan="4"><b>Nivel <?php echo $curso['data'][0]['nivel']; ?> - <?php echo $curso['data'][0]['docente']; ?></b></th></tr>
            <tr><th>Fecha de Inicio:</th><td><?php echo $curso['data'][0]['fecha_inicio']; ?></td><th>Horario:</th><td><?php echo $curso['data'][0]['horario']; ?></td></tr>
            <tr><th>fecha de Cierre:</th><td><?php echo $curso['data'][0]['fecha_cierre']; ?></td><th>Idioma:</th><td><?php echo $curso['data'][0]['idioma']; ?></td></tr>
        </tbody>
    </table>
    <br>
    <table style="margin: auto; width: 480px;">
      <tr>
      <th style="text-align: left;">Evaluaciones:</th>
      <th style="text-align: right;">
        <input type="button" onclick="flep_add_evaluacion();" value="Agregar">
        <input type="hidden" value="<?php echo ($found ? count($structure['data']): '0'); ?>" id="add_next">
        <input type="submit" form="form_evaluacion" value="Guardar">
      </th>
      </tr>
    </table>
    <p class="error" id="perror"></p>
    <form action="store.php?type=evaluacion&page=detalle_curso&search=id:<?php echo $_POST['id']; ?>" method="POST" id="form_evaluacion" name="form_evaluacion">
        <input type="hidden" value="<?php echo $_POST['id']; ?>" id="id" name="id">
        <table class="eval" id='table_eval'>
          <tr><th>N&deg;</th><th>Evaluacion</th><th>Porcentaje</th><td></td></tr>
          <?php
                      if($found){
                        for($i = 0; $i < count($structure['data']); $i++){
                          printf('<tr><th>%s</th>', $i+1);
                          printf('<td><input type="hidden" name="data[%s][id]" value="%s">', $i, flep_encode($structure['data'][$i]['id']));
                          printf('<input type="text" name="data[%s][evaluacion]" required value="%s"></td>', $i, $structure['data'][$i]['evaluacion']);
                          printf('<td><input type="text" size="3" name="data[%s][porcentaje]" required value="%s">&#37;</td>', $i, $structure['data'][$i]['porcentaje']);
                          //TODO: Del by JS
                          printf('<td style="text-align:center;padding:0px;"><a href="delete.php?type=evaluacion&id=%s"><img src="img/delete.png" title="Eliminar"></a></td></tr>', flep_encode($structure['data'][$i]['id']));
                        }
                      }else{
                          printf('<tr><td colspan="%s">No se han agregado evaluaciones - Haga click en Agregar.</td></tr>', count($structure['fields']) + 2);
                      }
                      ?>
                  </table>
              </td>
            </tr>
        <br>
    </form>
    <br>
<?php
}
?>
